home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CADSP 1.0 / Messenger Classes / CNBP.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  4.9 KB  |  271 lines  |  [TEXT/KAHL]

  1. /***
  2.  * CNBP
  3.  *
  4.  *        Name Binding Protocol handler
  5.  *
  6.  *        ask this object to handle names over appletalk
  7.  *
  8.  *        Copyright © 1992 Bernard Bernstein. All rights reserved.
  9.  ***/ 
  10.  
  11.  
  12. #include <TBUtilities.h>
  13. #include <TCLUtilities.h>
  14. #include <Global.h>
  15. #include <CArray.h>
  16.  
  17. #include "CNBP.h"
  18.  
  19.  
  20.  
  21. /***
  22.  * INBP
  23.  *
  24.  *        Initialize the NBP handler.
  25.  ***/
  26. void CNBP::INBP(void)
  27. {
  28.     MPPPBPtr thepb;
  29.     
  30.     if (!IsMPPOpen())
  31.         FailOSErr(MPPOpen());
  32.     if (!IsATPOpen())
  33.         FailOSErr(ATPLoad());
  34.  
  35.     FailOSErr(NBPLoad());
  36.     
  37.     thepb = (MPPPBPtr)NewPtr(sizeof(MPPParamBlock));
  38.     FailNIL(thepb);
  39.     
  40.     nte = (NamesTableEntry*)NewPtr(sizeof(NamesTableEntry));
  41.     FailNIL(nte);
  42.  
  43.     itsMppPb = thepb;
  44.     nameOnTable = false;
  45.     itsNamesSet = false;
  46.     itsSocketSet = false;
  47.     
  48. }
  49.  
  50. /***
  51.  * Dispose
  52.  *
  53.  *        Kill the structures created by this object.
  54.  ***/
  55. void CNBP::Dispose(void)
  56. {
  57.     if (nameOnTable)
  58.         Remove();
  59.  
  60.     ForgetPtr(itsMppPb);
  61.     ForgetPtr(nte);
  62.     inherited::Dispose();
  63. }
  64.  
  65.  
  66.  
  67.  
  68. /**********************************************************************
  69.  
  70.                             Misc
  71.             
  72. **********************************************************************/
  73.  
  74.  
  75. /***
  76.  * SetName
  77.  *
  78.  *        Set the name for the nbp object
  79.  ***/
  80. void CNBP::SetName(Str32 objName, Str32 typeName, Str32 zoneName)
  81. {
  82.     CopyPString(objName, itsObjName);
  83.     CopyPString(typeName, itsTypeName);
  84.     CopyPString(zoneName, itsZoneName);
  85.     itsNamesSet = true;
  86. }
  87.  
  88. /***
  89.  * SetSocket
  90.  *
  91.  *        Give a socket to attach to this nbp name
  92.  ***/
  93. void CNBP::SetSocket(short socket)
  94. {
  95.     itsSocket = socket;
  96.     itsSocketSet = true;
  97. }
  98.  
  99. /***
  100.  * MatchNames
  101.  *
  102.  *        Fill the array with the names found
  103.  *        matches is an allocate, but not initialized CArray
  104.  *
  105.  *        It gets filled with NBPAddrNameRec objects which
  106.  *        are defined in CNBP.h, the header for this file
  107.  ***/
  108. void CNBP::MatchNames(Str32 objMatch, Str32 typeMatch, Str32 zoneMatch, 
  109.                             CArray *matches)
  110. {
  111.     short i;
  112.     EntityName anEntity;
  113.     AddrBlock anAddr;
  114.     short numGotten;
  115.     NBPAddrNameRec rec;
  116.     unsigned char *p;
  117.     Ptr nameBuf = nil;
  118.     short bufSize;
  119.     short maxToGet;
  120.     
  121.     ASSERT(matches && member(matches, CArray));
  122.  
  123.     TRY
  124.         {
  125.         
  126.         // setup the buffer to store the matching names
  127.         maxToGet = 50;
  128.         bufSize = maxToGet * (sizeof(EntityName) + sizeof(AddrBlock));
  129.         nameBuf = NewPtr(bufSize);
  130.         FailNIL(nameBuf);
  131.         
  132.         
  133.         // lookup the names and fill the buffer with them
  134.         Lookup(objMatch, typeMatch, zoneMatch, nameBuf, bufSize, maxToGet);
  135.         numGotten = itsMppPb->NBPnumGotten;
  136.         
  137.         if (numGotten > 0)
  138.             {
  139.             for(i=1; i<=itsMppPb->NBPnumGotten; i++)
  140.                 {
  141.                 NBPExtract(itsMppPb->NBPretBuffPtr, itsMppPb->NBPnumGotten, i,
  142.                             &anEntity, &anAddr);
  143.                 BlockMove(anEntity.objStr, rec.name, 32);
  144.                 BlockMove(anEntity.typeStr, rec.type, 32);
  145.                 BlockMove(anEntity.zoneStr, rec.zone, 32);
  146.                 rec.addr = anAddr;
  147.                 matches->InsertAtIndex( &rec, matches->GetNumItems()+1);
  148.                 }
  149.             }
  150.         ForgetPtr(nameBuf);
  151.         }
  152.     CATCH
  153.         {
  154.         ForgetPtr(nameBuf);
  155.         }
  156.     ENDTRY;
  157.     
  158. }
  159.  
  160.  
  161. /**********************************************************************
  162.  
  163.                             NBP methods
  164.             
  165. **********************************************************************/
  166.  
  167.  
  168. /***
  169.  * Register
  170.  *
  171.  *        Register this objects name for nbp
  172.  ***/
  173. void CNBP::Register(void)
  174. {
  175.     ASSERT(itsSocketSet && (itsSocket != 0));
  176.     ASSERT(itsNamesSet);
  177.  
  178.     NBPSetNTE((Ptr)nte, (Ptr)itsObjName, (Ptr)itsTypeName, 
  179.                         (Ptr)itsZoneName, itsSocket);
  180.  
  181.     itsMppPb->NBPntQElPtr = (Ptr) nte;
  182.     itsMppPb->NBPinterval = 7;
  183.     itsMppPb->NBPcount = 3;
  184.     itsMppPb->NBPverifyFlag = true;
  185.     
  186.     FailOSErr(PRegisterName(itsMppPb, false));
  187.     nameOnTable = true;
  188. }
  189.  
  190. /***
  191.  * Lookup
  192.  *
  193.  *        Lookup the object
  194.  ***/
  195. void CNBP::Lookup(Str32 objMatch, Str32 typeMatch, Str32 zoneMatch,
  196.                     void *buffer, short size, short maxToGet)
  197. {
  198.     EntityPtr mEntity = nil;
  199.     
  200.     mEntity = (EntityPtr)NewPtr(sizeof(EntityName));
  201.     FailNIL(mEntity);
  202.  
  203.     TRY
  204.         {
  205.         
  206.         NBPSetEntity((Ptr)mEntity, (Ptr)objMatch, (Ptr)typeMatch, (Ptr)zoneMatch);
  207.     
  208.         itsMppPb->NBPentityPtr = (Ptr) mEntity;
  209.         itsMppPb->NBPinterval = 5;
  210.         itsMppPb->NBPcount = 5;
  211.         itsMppPb->NBPretBuffPtr = (Ptr) buffer;
  212.         itsMppPb->NBPretBuffSize = size;
  213.         itsMppPb->NBPmaxToGet = maxToGet;
  214.         
  215.         FailOSErr(PLookupName(itsMppPb, false));
  216.         
  217.         ForgetPtr(mEntity);
  218.         }
  219.     CATCH
  220.         {
  221.         ForgetPtr(mEntity);
  222.         }
  223.     ENDTRY;
  224. }
  225.  
  226.  
  227.  
  228. /***
  229.  * Remove
  230.  *
  231.  *        Remove name from table
  232.  ***/
  233. void CNBP::Remove(void)
  234. {
  235.     EntityPtr mEntity = nil;
  236.  
  237.     ASSERT(itsNamesSet);
  238.  
  239.     mEntity = (EntityPtr)NewPtr(sizeof(EntityName));
  240.     FailNIL(mEntity);
  241.  
  242.     NBPSetEntity((Ptr)mEntity, (Ptr)itsObjName, (Ptr)itsTypeName, (Ptr)itsZoneName);
  243.     
  244.     itsMppPb->NBPentityPtr = (Ptr)mEntity;
  245.     
  246.     FailOSErr(PRemoveName(itsMppPb, false));
  247.     ForgetPtr(mEntity);
  248.     nameOnTable = false;
  249. }
  250.  
  251.  
  252.  
  253. /***
  254.  * SetSelfSend
  255.  *
  256.  *        pass a boolean to set whether we see ourself or not
  257.  *        returns what we were before setting it
  258.  ***/
  259. Boolean CNBP::SetSelfSend(Boolean selfsend)
  260. {
  261.     MPPPBPtr self;
  262.     Boolean result;
  263.     
  264.     self = (MPPPBPtr)NewPtr(sizeof(MPPParamBlock));
  265.  
  266.     self->SETSELF.newSelfFlag = selfsend ? 1 : 0;
  267.     FailOSErr(PSetSelfSend(self, false));
  268.     result = self->SETSELF.oldSelfFlag != 0;
  269.     ForgetPtr(self);
  270. }
  271.